package com.lc.ibps.platform.rest.auth;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Maps;
import com.lc.ibps.api.base.model.User;
import com.lc.ibps.base.core.util.AppUtil;
import com.lc.ibps.base.web.context.ContextUtil;
import com.lc.ibps.cloud.entity.APIResult;
import com.lc.ibps.org.app.persistence.entity.AppresPo;
import com.lc.ibps.org.app.repository.AppresRepository;
import com.lc.ibps.org.app.service.AppresQueryService;
import com.lc.ibps.org.party.persistence.entity.DefaultPartyRolePo;
import com.lc.ibps.org.party.repository.DefaultPartyRoleRepository;
import com.lc.ibps.web.controller.BaseApiController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 *资源服务
 *
 * <pre> 
 * 构建组：ibps-platform-webapi
 * 作者：hugh zhuang
 * 邮箱：3378340995@qq.com
 * 日期：2018年8月29日-下午5:00:06
 * 版权：广州流辰信息技术有限公司版权所有
 * </pre>
 */
@RequestMapping(value = "/api/webapi/appResourcesService")
@Api(value = "/appResourcesService",tags = "系统管理-菜单服务")
@RestController
public class AppResourcesController extends BaseApiController {

	private DefaultPartyRoleRepository partyRoleRepository;
	private AppresQueryService appresQueryService;
	private AppresRepository appresRepository;
	
	public AppResourcesController() {
		partyRoleRepository = AppUtil.getBean(DefaultPartyRoleRepository.class);
		appresQueryService = AppUtil.getBean(AppresQueryService.class);
		appresRepository = AppUtil.getBean(AppresRepository.class);
	}

	/**
	 * 
	 * 获取菜单的数据
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "获取菜单的数据", notes = "获取菜单的数据")
	@RequestMapping(value = "/getMenuData", method = RequestMethod.GET)
	public APIResult<Map<String, Object>> getMenuData() throws Exception {
		APIResult<Map<String, Object>> result = new APIResult<Map<String, Object>>();
		User user = ContextUtil.getCurrentUser();
		try {
			// 查询角色
		
			List<AppresPo> resList = new ArrayList<>();
			Map<String, AppresPo> resMap = new LinkedHashMap<>();

			if (user.isSuper()) {
				String listData = appresQueryService.findAll();
				List<AppresPo> list = AppresPo.fromJsonArrayString(listData);
				resList.addAll(list);
			} else {

				List<DefaultPartyRolePo> roleList = partyRoleRepository.findUserRolesByUserId(user.getUserId());
				if (CollectionUtils.isNotEmpty(roleList)) {
					for (DefaultPartyRolePo role : roleList) {
						List<AppresPo> list = appresRepository.findRoleResTreeList(role.getId(), AppresPo.DEFAULT_SUBSYSTEM_ID);
						for (AppresPo po : list) {
							resMap.put(po.getId(), po);
						}
					}
					if (resMap.size() > 0)
						resList.addAll(resMap.values());
				}
			}
			Map<String, Object> data = Maps.newHashMap();
			data.put("menus",resList);
			result.setData(data);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取菜单的数据失败！",e);
		}
		return result;
	}

}
